def monitor_parsing_health(sample_size=20):
    """监控解析器健康状况"""
    crawler = AmazonCrawler()
    parser = AdaptiveAmazonParser()
    
    # 获取最近的ASIN样本
    recent_asins = FollowPriceXue.objects.order_by('-opdate')[:sample_size].values_list('itemid', flat=True)
    
    results = {
        'total': len(recent_asins),
        'success': 0,
        'failures': [],
        'field_stats': {}
    }
    
    for asin in recent_asins:
        try:
            url = f"https://www.amazon.com/dp/{asin}"
            html = crawler.download_product_page(url)
            
            if not html:
                results['failures'].append({'asin': asin, 'reason': 'Download failed'})
                continue
                
            parsed = parser.parse_product_page(html, url)
            
            # 统计各字段解析成功率
            for field in ['title', 'price', 'images', 'rating', 'reviews']:
                if field not in results['field_stats']:
                    results['field_stats'][field] = {'success': 0, 'total': 0}
                
                results['field_stats'][field]['total'] += 1
                if field in parsed and parsed[field]:
                    results['field_stats'][field]['success'] += 1
            
            results['success'] += 1
        except Exception as e:
            results['failures'].append({'asin': asin, 'reason': str(e)})
    
    # 计算各字段成功率
    for field, stats in results['field_stats'].items():
        stats['rate'] = stats['success'] / stats['total'] if stats['total'] > 0 else 0
        
        # 检查是否需要告警
        if stats['rate'] < 0.7:  # 低于70%成功率时告警
            send_alert(f"解析器{field}字段成功率低: {stats['rate']:.1%}")
    
    return results 